package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/authorInfo")
public class AuthorServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过blogId获取作者名字
        resp.setContentType("application/json; charset=utf-8");
        String blogId = req.getParameter("blogId");
        if (blogId == null || "".equals("blogId")){
            resp.getWriter().write("{ \"ok\": false, \"reason\": \"参数缺失!\" }");
            return;
        }

        //先根据BlogId获取博客对象
        BlogDao blogDao = new BlogDao();
        Blog blog = blogDao.selectOne(Integer.parseInt(blogId));
        if (blog == null){
            resp.getWriter().write("{ \"ok\": false, \"reason\": \"查询的博客不存在!\" }");
            return;
        }
        //再根据博客对象获取用户对象
        UserDao userDao = new UserDao();
        User user = userDao.selectById(blog.getUserId());
        if (user == null){
            resp.getWriter().write("{ \"ok\": false, \"reason\": \"查询的用户不存在!\" }");
            return;
        }

        user.setPassword("");
        resp.getWriter().write(objectMapper.writeValueAsString(user));
    }
}
